﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;

namespace CooStationsToolsNew
{
    /// <summary>
    /// CheckEdit.xaml 的交互逻辑
    /// </summary>
    public partial class CheckEdit : Window
    {
        string cfyevent = App.appPath + "\\temp\\" + App.pageName + "_event.xml";

        public CheckEdit()
        {
            InitializeComponent();
            this.windowsFormsHost1.Child = text;
            text.ErrorChange += new Action<List<Check.ErrorData>>(text_ErrorChange);
            
            font = editfont;
            if (font.Font == null || font.Font.Length == 0)
            {
                font.Font = this.text.Font.Name;
            }
            this.setZH(font.FontSize);
            this.setZT(font.Font);
            init();
            this.Closed += new EventHandler(CheckEdit_Closed);
            Loadthis(cfyevent);
        }

        private void Loadthis(string url)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(url);

            XmlNodeList list = doc.ChildNodes[1].ChildNodes;

            string n = "";
            foreach (XmlNode x in list)
            {
                if (x.Name == "Scripts")
                {
                    n = x.FirstChild.InnerText;
                    break;
                }
            }
            text.Text = n;
            //richTextBox1.AppendText(n);
        }

        void CheckEdit_Closed(object sender, EventArgs e)
        {
            Save();
        }

        private EditFont editfont
        {
            get { return XMLInfoOP<EditFont>.GetInfo(AppDomain.CurrentDomain.BaseDirectory + "font"); }
            set { XMLInfoOP<EditFont>.SaveInfo(value, AppDomain.CurrentDomain.BaseDirectory + "font"); }
        }

        private EditFont font = new EditFont();

        private Check.EditText text = new Check.EditText();
        private string GetHL(int index)
        {
            int h = this.text.GetLineFromCharIndex(index);
            int l = index - this.text.GetFirstCharIndexFromLine(h);
            return string.Format("在{0}行{1}列处", h + 1, l + 1);
        }

        void text_ErrorChange(List<Check.ErrorData> obj)
        {
            this.listBox1.Items.Clear();
            foreach (Check.ErrorData one in obj)
            {
                this.listBox1.Items.Add(one.Name + "    " + GetHL(one.Num));
            }
        }

        void init()
        {
            var m1 = new MenuItem() { Header = "文件" };
            var m2 = new MenuItem() { Header = "显示" };
            this.menu1.Items.Add(m1);
            this.menu1.Items.Add(m2);
            var save = new MenuItem() { Header = "保存" };
            var close = new MenuItem() { Header = "关闭" };
            close.Click += new RoutedEventHandler(close_Click);
            save.Click += new RoutedEventHandler(save_Click);
            m1.Items.Add(save);
            m1.Items.Add(close);
            var zt = new MenuItem() { Header = "字体" };
            var zh = new MenuItem() { Header = "字号" };

            m2.Items.Add(zt);
            m2.Items.Add(zh);
            for (int i = 8; i < 30; i++)
            {
                var h = new MenuItem() { Header = i.ToString() };
                h.Click += new RoutedEventHandler(h_Click);
                zh.Items.Add(h);
            }
            System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
            foreach (System.Drawing.FontFamily family in fonts.Families)
            {
                var t = new MenuItem() { Header = family.Name };
                t.Click += new RoutedEventHandler(t_Click);
                zt.Items.Add(t);
            }




        }

        void close_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        void t_Click(object sender, RoutedEventArgs e)
        {
            var tem = sender as MenuItem;
            if (tem != null)
            {
                setZT(tem.Header.ToString());
            }
        }

        void h_Click(object sender, RoutedEventArgs e)
        {
            var tem = sender as MenuItem;
            if (tem != null)
            {
                setZH(Convert.ToInt32(tem.Header));
            }

        }

        void save_Click(object sender, RoutedEventArgs e)
        {
            Save();
        }

        void Save()
        {
            ProjectTools pt = new ProjectTools();
            pt.SetScript(text.Text);
        }

        void setZT(string zt)
        {
            this.text.SetFont(zt);
            this.font.Font = zt;
            editfont = this.font;
        }

        void setZH(int zh)
        {
            this.text.SetFontSize(zh);
            this.font.FontSize = zh;
            editfont = this.font;
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void lbtn_close_Click(object sender, RoutedEventArgs e)
        {
            Save();
            this.Close();
        }

        private void lbtn_max_Click(object sender, RoutedEventArgs e)
        {
            if (MainWindow.CheckWindowsVersion() == "Windows7")
            {
                this.MaxHeight = SystemParameters.WorkArea.Height + 6;
            }
            else
            {
                this.MaxHeight = SystemParameters.WorkArea.Height;
            }
            Style style1 = (Style)this.FindResource("StyleBtn5");
            Style style2 = (Style)this.FindResource("StyleBtn5_other");
            this.WindowState = (this.WindowState == System.Windows.WindowState.Normal ? System.Windows.WindowState.Maximized : System.Windows.WindowState.Normal);
            lbtn_max.Style = (lbtn_max.Style == style1 ? style2 : style1);
        }

        private void lbtn_min_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Minimized;
        }

    }



    public class EditFont
    {
        public EditFont()
        {
            this.FontSize = 9;
        }

        public string Font { get; set; }

        public int FontSize { get; set; }
    }
}
